package com.yfl.logistics.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfl.logistics.pojo.MsgObject;
import com.yfl.logistics.pojo.Role;
import com.yfl.logistics.pojo.Permission;
import com.yfl.logistics.service.impl.RoleServiceImpl;
import com.yfl.logistics.service.impl.PermissionServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-13
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionServiceImpl permissionService;



    //权限页面跳转
    @RequiresPermissions("permission:permissionPage")
    @GetMapping("/permissionPage")
    public String permissionPage() {

        return "permissionPage";
    }


    //分页查询操作
    @RequiresPermissions("permission:list")
    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    String keyword) {

        //创建分页插件
        IPage<Permission> page = new Page<>(pageNum, pageSize);

        //创建条件查询对象
        QueryWrapper qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            qw.like("c.name", keyword);
            qw.or();
            qw.like("p.name", keyword);
        }

        IPage<Permission> iPage = permissionService.selectPermission(page, qw);

        Map<String, Object> map = new HashMap<>();
        map.put("total", iPage.getTotal());
        map.put("rows", iPage.getRecords());

        return map;
    }

    //删除操作

    @RequiresPermissions("permission:delete")
    @GetMapping("/delete")
    @ResponseBody
    public MsgObject delect(Integer permissionId) {

        QueryWrapper<Permission> qw = new QueryWrapper<>();
        qw.eq("permission_id", permissionId);
        boolean remove = permissionService.remove(qw);
        MsgObject msgObject = new MsgObject();
        if (remove) {
            msgObject.setCode(1);
            msgObject.setMsg("删除成功");
        } else {
            msgObject.setCode(0);
            msgObject.setMsg("失败");
        }

        return msgObject;
    }

    //添加编辑页面跳转
    @GetMapping("/permissionEdit")
    public String permissionEdit(Integer permissionId, Model m) {


        QueryWrapper<Permission> qw =new QueryWrapper<>();
        qw.eq("permission_id",permissionId);
        Permission permission = permissionService.getOne(qw);

        //编辑数据回显
        m.addAttribute("permission",permission);

        //父权限回显
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type","menu");
        List<Permission> list = permissionService.list(queryWrapper);
        m.addAttribute("pNames",list);

        return "permissionEdit";
    }


    //添加/更新权限
    @RequiresPermissions("permission:update")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public MsgObject saveOrUpdatePermission(Permission permission) {

        boolean saveOrUpdate = permissionService.saveOrUpdate(permission);

        MsgObject msgObject = new MsgObject();
        if (saveOrUpdate) {
            msgObject.setMsg("操作成功");
            msgObject.setCode(1);
        } else {
            msgObject.setMsg("操作失败");
            msgObject.setCode(2);
        }
         
        return msgObject;
    }

    //批量删除
    @RequiresPermissions("permission:delete")
    @GetMapping("/delpermissions")
    @ResponseBody
    public MsgObject delpermissions(@RequestParam Integer[] permissionIds) {

        boolean removeByIds = permissionService.removeByIds(Arrays.asList(permissionIds));

        MsgObject msgObject = new MsgObject();
        if (removeByIds) {
            msgObject.setMsg("批量删除成功");
            msgObject.setCode(1);
        } else {
            msgObject.setMsg("批量删除失败");
            msgObject.setCode(2);
        }
        return msgObject;
    }


    //获取全部数据
    @PostMapping("/getAllpermission")
    @ResponseBody
    public List<Permission> getAllpermission(){

        List<Permission> list = permissionService.list();
        return list;
    }
}

